﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.Doc;
using UFIDA.U9.CBO.SCM.Enums;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFIDA.U9.PM.ASN;
using UFIDA.U9.PM.PO;
using UFIDA.U9.SM.RMR;
using UFIDA.U9.SM.ShipPlan;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.RTNP
{
    public class AfterUpdating : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.PM.Rtn.Rtn;
            if (holder == null)
                return;
            #endregion
            //审核
            if (holder.Status == PM.Rtn.RtnStatusEnum.Approved && holder.OriginalData.Status == PM.Rtn.RtnStatusEnum.Approving)
            {

                if (holder.DocType.BusinessType != BusinessTypeEnum.PM055)
                {
                    HttpHelper httpHelper = new HttpHelper();
                    JsonConfig config = new JsonConfig();
                    httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/create-delivery-note";



                    DeliveryNoteCreateOpenV3Req dto = new DeliveryNoteCreateOpenV3Req();

                    dto.tenantId = "1";
                    dto.sourceSysType = "ERP";
                    dto.billGroupCode = "CGC01";
                    dto.billTypeCode = "CGC01-01";

                    dto.billSourceCode = holder.RtnDocType.Code;
                    dto.billSourceText = holder.RtnDocType.Name;
                    dto.billSourceNumber = holder.DocNo;
                    dto.urgencyDegree = "NORMAL";
                    dto.targetType = "TARGET_SUPPLIER";
                    dto.targetCode = holder.RtnToSupplier.Supplier.Code;
                    dto.plannedTime = holder.BusinessDate.ToString();
                    dto.completionTime = holder.BusinessDate.ToString();
                    dto.remark = holder.Memo;

                    List<DeliveryRequirementCreateOpenReq> dtolines = new List<DeliveryRequirementCreateOpenReq>();

                    foreach (var line in holder.RtnLine)
                    {



                        DeliveryRequirementCreateOpenReq dtoit = new DeliveryRequirementCreateOpenReq();
                        dtoit.materialCode = line.ItemInfo.ItemCode;
                        dtoit.supplierCode = holder.RtnToSupplier.Supplier.Code;
                        dtoit.batchNo = line.LotMaster?.LotCode ?? "";
                        dtoit.warehouseCode = line.WH?.Code ?? "";
                        dtoit.productionDate = holder.BusinessDate.ToString();
                        dtoit.entryTime = holder.BusinessDate.ToString();
                        dtoit.qualityLevel = "QUALIFIED_PRODUCT";
                        dtoit.lineNumber = line.RtnLineNO.ToString();
                        dtoit.plannedQuantity = line.RtnQtyRU;
                        dtoit.barcodeQuantity = line.RtnQtyRU;
                        dtoit.sourceBillCode = holder.DocNo;
                        dtoit.sourceDetailLineNumber = line.RtnLineNO.ToString();
                        dtolines.Add(dtoit);
                    }

                    dto.requirements = dtolines;

                    httpHelper.Content = JsonConvert.SerializeObject(dto);
                    var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                    if (result["code"].ToString() != "000000")
                    {
                        throw new Exception(result["msg"].ToString());
                    }
                }
                else
                {
                    HttpHelper httpHelper = new HttpHelper();
                    JsonConfig config = new JsonConfig();
                    httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/create-delivery-note";


                    DeliveryNoteCreateOpenV3Req dto = new DeliveryNoteCreateOpenV3Req();

                    dto.tenantId = "1";
                    dto.sourceSysType = "ERP";

                    dto.billGroupCode = "WWC02";
                    dto.billTypeCode = "WWC02-01";
                    dto.billSourceCode = holder.RtnDocType.Code;
                    dto.billSourceText = holder.RtnDocType.Name;
                    dto.billSourceNumber = holder.DocNo;
                    dto.urgencyDegree = "NORMAL";
                    dto.targetType = "TARGET_SUPPLIER";
                    dto.targetCode = holder.RtnToSupplier.Supplier.Code;
                    dto.plannedTime = holder.BusinessDate.ToString();
                    dto.completionTime = holder.BusinessDate.ToString();
                    dto.remark = holder.Memo;

                    List<DeliveryRequirementCreateOpenReq> dtolines = new List<DeliveryRequirementCreateOpenReq>();

                    foreach (var line in holder.RtnLine)
                    {
                        DeliveryRequirementCreateOpenReq dtoit = new DeliveryRequirementCreateOpenReq();
                        dtoit.materialCode = line.ItemInfo.ItemCode;
                        dtoit.supplierCode = holder.RtnToSupplier.Supplier.Code;
                        dtoit.batchNo = line.LotMaster?.LotCode ?? "";
                        dtoit.warehouseCode = line.WH?.Code ?? "";
                        dtoit.productionDate = holder.BusinessDate.ToString();
                        dtoit.entryTime = holder.BusinessDate.ToString();
                        dtoit.qualityLevel = "QUALIFIED_PRODUCT";
                        dtoit.lineNumber = line.RtnLineNO.ToString();
                        dtoit.plannedQuantity = line.RtnQtyRU;
                        dtoit.barcodeQuantity = line.RtnQtyRU;
                        dtoit.sourceBillCode = holder.DocNo;
                        dtoit.sourceDetailLineNumber = line.RtnLineNO.ToString();
                        dtolines.Add(dtoit);
                    }

                    dto.requirements = dtolines;

                    httpHelper.Content = JsonConvert.SerializeObject(dto);
                    var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                    if (result["code"].ToString() != "000000")
                    {
                        throw new Exception(result["msg"].ToString());
                    }
                }
            }

            //弃审
            if (holder.Status == PM.Rtn.RtnStatusEnum.Opened && holder.OriginalData.Status == PM.Rtn.RtnStatusEnum.Approved)
            {
                HttpHelper httpHelper = new HttpHelper();
                JsonConfig config = new JsonConfig();
                httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/delete-delivery-note";


                WMSDeleteDTO dto = new WMSDeleteDTO();
                dto.billSourceNumber = holder.DocNo;

                httpHelper.Content = JsonConvert.SerializeObject(dto);
                var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                if (result["code"].ToString() != "000000")
                {
                    throw new Exception(result["msg"].ToString());
                }
            }
        }
    }
}
